package com.tjp.eight.ui.fragment.commentdetails;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.comment.CommentModel;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.CircleImageView;
import com.tjp.eight.views.emoj.EmojiUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2017/2/24.
 */
public class CommentDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private CommentDetailResultModel resultModel;
    private ItemClcik itemClcik;
    private OnNameCLickDetail onNameCLickDetail;
    private ItemCommentDetailsZanClick commentDetailsZanClick;
    private final int HEAD = 0;
    private final int NORMAL = 1;
    private String coid = "";

    public CommentDetailsAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEAD:
                View headView = LayoutInflater.from(context).inflate(R.layout.comment_detail_head, parent, false);
                return new HeadCommentDetailsViewHolder(headView);
            case NORMAL:
                View normalView = LayoutInflater.from(context).inflate(R.layout.item_comment_details_item, parent, false);
                return new CommentDetailsViewHolder(normalView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (resultModel == null) return;
        switch (getItemViewType(position)) {
            case HEAD:
                final String uid = resultModel.uid;
                ((HeadCommentDetailsViewHolder) holder).rl_comment_zan_who.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.startWhoZan_(context, coid);
                    }
                });
                ((HeadCommentDetailsViewHolder) holder).ll_comment_a.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.startDetails(context, resultModel.data.contentdata.cid);
                    }
                });
                if (resultModel.data == null || resultModel.data.comment_list == null || resultModel.data.comment_list.isEmpty()) {
                    (((HeadCommentDetailsViewHolder) holder).ll_comment_detail_new_tips).setVisibility(View.GONE);
                } else {
                    (((HeadCommentDetailsViewHolder) holder).ll_comment_detail_new_tips).setVisibility(View.VISIBLE);
                }
                (((HeadCommentDetailsViewHolder) holder).ll_who_zan).removeAllViews();
                if (resultModel.data == null || resultModel.data.praiseuser == null || resultModel.data.praiseuser.isEmpty()) {
                    (((HeadCommentDetailsViewHolder) holder).rl_comment_zan_who).setVisibility(View.GONE);
                } else {
                    (((HeadCommentDetailsViewHolder) holder).rl_comment_zan_who).setVisibility(View.VISIBLE);
                    for (CommentDetailResultModel.PraiseModel praiseModel : resultModel.data.praiseuser) {
                        final String uids = praiseModel.uid;
                        CircleImageView xview = (CircleImageView) LayoutInflater.from(context).inflate(R.layout.who_zan_comment_detail, (((HeadCommentDetailsViewHolder) holder).ll_who_zan), false);
                        xview.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //点击头像进入个人详情页
                                ActivityUtils.startOtherHomePage(context, uids);
                            }
                        });
                        Glide.with(context).load(praiseModel.iconurl).error(R.drawable.icon_login).into(xview);
                        (((HeadCommentDetailsViewHolder) holder).ll_who_zan).addView(xview);
                    }
                }
                (((HeadCommentDetailsViewHolder) holder)).iv_comment_zan.setImageBitmap(resultModel.if_praise == 0 ?
                        BitmapUtils.getBitmap(context, R.drawable.like_un) : BitmapUtils.getBitmap(context, R.drawable.like));
                (((HeadCommentDetailsViewHolder) holder)).ll_to_zan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (commentDetailsZanClick != null) {
                            commentDetailsZanClick.ItemDetailZanClick(position, coid);
                        }
                    }
                });
                (((HeadCommentDetailsViewHolder) holder)).ll_to_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClcik != null) {
                            itemClcik.repeat(coid, resultModel.name);
                        }
                    }
                });

                (((HeadCommentDetailsViewHolder) holder).civ_head_comment_details).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.startOtherHomePage(context, uid);
                    }
                });
                Glide.with(context).load(resultModel.iconurl).error(R.drawable.icon_login).into((((HeadCommentDetailsViewHolder) holder).civ_head_comment_details));
                (((HeadCommentDetailsViewHolder) holder)).tv_name.setText(resultModel.name);
                (((HeadCommentDetailsViewHolder) holder)).tv_time.setText(resultModel.passtime);
                try {
                    new EmojiUtil().handlerEmojiText((((HeadCommentDetailsViewHolder) holder)).tv_comment_content, resultModel.detail, context);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Glide.with(context).load(resultModel.data.contentdata.cover_path).error(R.drawable.loading).into((((HeadCommentDetailsViewHolder) holder)).iv_pic);
                (((HeadCommentDetailsViewHolder) holder)).tv_title.setText(resultModel.data.contentdata.title);

                break;
            case NORMAL:
                final int pos = position - 1;
                final String uid2 = resultModel.data.comment_list.get(pos).uid;
                Glide.with(context).load(resultModel.data.comment_list.get(pos).iconurl)
                        .error(R.drawable.icon_login).into((((CommentDetailsViewHolder) holder)).civ_u_pic_hot_comment);
                (((CommentDetailsViewHolder) holder)).civ_u_pic_hot_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.startOtherHomePage(context, uid2);
                    }
                });
                (((CommentDetailsViewHolder) holder)).tv_user_name.setText(resultModel.data.comment_list.get(pos).name + "");
                (((CommentDetailsViewHolder) holder)).tv_pass_time.setText(resultModel.data.comment_list.get(pos).pass_time);
                (((CommentDetailsViewHolder) holder)).tv_comment_count.setText("" + StringUtils.toWan(resultModel.data.comment_list.get(pos).com_time) + "");
                (((CommentDetailsViewHolder) holder)).tv_zan_count.setText("" + StringUtils.toWan(resultModel.data.comment_list.get(pos).praise));
                String detail = "回复" + resultModel.data.comment_list.get(pos).parent_name + ":";
                int len = 0;
                if (!StringUtils.isNullOrEmpty(resultModel.data.comment_list.get(pos).parent_name)) {
                    len = resultModel.data.comment_list.get(pos).parent_name.length() + 2;
                } else {
                    len = 2;
                }
                SpannableString spannableString = new SpannableString(detail);
                spannableString.setSpan(new Clickable(resultModel.data.comment_list.get(pos).parent_uid), 2,
                        len,
                        Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                (((CommentDetailsViewHolder) holder)).tv_comment_detail_item_content.setText(spannableString);
                String msg = resultModel.data.comment_list.get(pos).detail;
                try {
                    new EmojiUtil().handlerEmojiTextAppend((((CommentDetailsViewHolder) holder)).tv_comment_detail_item_content, msg, context);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                (((CommentDetailsViewHolder) holder)).ll_zan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (commentDetailsZanClick != null) {
                            commentDetailsZanClick.ItemDetailZanClick(position, resultModel.data.comment_list.get(pos).coid);
                        }
                    }
                });
                (((CommentDetailsViewHolder) holder)).iv_comment_zan.setImageBitmap(resultModel.data.comment_list.get(pos).if_praise == 0 ?
                        BitmapUtils.getBitmap(context, R.drawable.like_un) : BitmapUtils.getBitmap(context, R.drawable.like));
                (((CommentDetailsViewHolder) holder)).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (IsSuccessUtils.isMySelf(resultModel.data.comment_list.get(pos).uid))
                            return;
                        if (itemClcik != null) {
                            itemClcik.repeat(resultModel.data.comment_list.get(pos).coid, resultModel.data.comment_list.get(pos).name);
                        }
                    }
                });
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return HEAD;
        return NORMAL;
    }

    @Override
    public int getItemCount() {
        return resultModel == null || resultModel.data == null || resultModel.data.comment_list == null ? 1 : 1 + resultModel.data.comment_list.size();
    }

    public void setItemClcik(ItemClcik itemClcik) {
        this.itemClcik = itemClcik;
    }

    public void setModel(CommentDetailResultModel model) {
        this.resultModel = model;
        notifyDataSetChanged();
    }

    public void addItemModel(CommentDetailResultModel model) {
        if (model == null || model.data == null || model.data.comment_list == null || model.data.comment_list.isEmpty())
            return;
        if (resultModel.data == null) return;
        if (resultModel.data.comment_list == null) {
            resultModel.data.comment_list = model.data.comment_list;
        } else {
            for (CommentModel commentListModel : model.data.comment_list) {
                resultModel.data.comment_list.add(commentListModel);
            }
        }
        notifyDataSetChanged();
    }

    public void zanPosition(int pos, int do_type, int praise) {
        if (pos == 0) {
            resultModel.if_praise = do_type;
        } else {
            resultModel.data.comment_list.get(pos - 1).if_praise = do_type;
            resultModel.data.comment_list.get(pos - 1).praise = praise;
        }

        notifyItemChanged(pos);
    }

    public void setCoid(String coid) {
        this.coid = coid;
    }

    public void addComment(CommentModel model) {
        if (model == null)
            return;
        if (resultModel.data == null) return;
        if (resultModel.data.comment_list == null) {
            resultModel.data.comment_list = new ArrayList<>();
        }
        resultModel.data.comment_list.add(model);
        notifyItemChanged(getItemCount());
        notifyDataSetChanged();

    }

    public void setOnNameCLickDetail(OnNameCLickDetail onNameCLickDetail) {
        this.onNameCLickDetail = onNameCLickDetail;
    }

    public void setCommentDetailsZanClick(ItemCommentDetailsZanClick commentDetailsZanClick) {
        this.commentDetailsZanClick = commentDetailsZanClick;
    }

    private String getTimeString(int time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }

    private class CommentDetailsViewHolder extends RecyclerView.ViewHolder {
        CircleImageView civ_u_pic_hot_comment;
        TextView tv_user_name;
        TextView tv_pass_time;
        TextView tv_comment_detail_item_content;
        TextView tv_comment_count;
        TextView tv_zan_count;
        ImageView iv_comment_zan;
        LinearLayout ll_zan;

        View itemView;

        public CommentDetailsViewHolder(View view) {
            super(view);
            itemView = view;
            civ_u_pic_hot_comment = (CircleImageView) view.findViewById(R.id.civ_u_pic_hot_comment);
            tv_user_name = (TextView) view.findViewById(R.id.tv_user_name);
            tv_pass_time = (TextView) view.findViewById(R.id.tv_pass_time);
            tv_comment_detail_item_content = (TextView) view.findViewById(R.id.tv_comment_detail_item_content);
            tv_comment_count = (TextView) view.findViewById(R.id.tv_comment_count);
            tv_zan_count = (TextView) view.findViewById(R.id.tv_zan_count);
            iv_comment_zan = (ImageView) view.findViewById(R.id.iv_comment_zan);
            ll_zan = (LinearLayout) view.findViewById(R.id.ll_zan);
        }
    }

    private class HeadCommentDetailsViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_comment_detail_new_tips;
        LinearLayout ll_comment_a;
        LinearLayout ll_who_zan;
        LinearLayout ll_to_comment;
        LinearLayout ll_to_zan;
        CircleImageView civ_head_comment_details;
        RelativeLayout rl_comment_zan_who;
        TextView tv_name;
        TextView tv_time;
        TextView tv_comment_content;
        TextView tv_title;
        TextView tv_comment_count;
        TextView tv_zan_count;
        ImageView iv_pic;
        ImageView iv_comment_zan;

        public HeadCommentDetailsViewHolder(View view) {
            super(view);
            ll_comment_detail_new_tips = (LinearLayout) view.findViewById(R.id.ll_comment_detail_new_tips);
            ll_comment_a = (LinearLayout) view.findViewById(R.id.ll_comment_a);
            ll_who_zan = (LinearLayout) view.findViewById(R.id.ll_who_zan);
            ll_to_zan = (LinearLayout) view.findViewById(R.id.ll_to_zan);
            ll_to_comment = (LinearLayout) view.findViewById(R.id.ll_to_comment);
            civ_head_comment_details = (CircleImageView) view.findViewById(R.id.civ_head_comment_details);
            rl_comment_zan_who = (RelativeLayout) view.findViewById(R.id.rl_comment_zan_who);

            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_comment_content = (TextView) view.findViewById(R.id.tv_comment_content);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_comment_count = (TextView) view.findViewById(R.id.tv_comment_count);
            tv_zan_count = (TextView) view.findViewById(R.id.tv_zan_count);
            iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
            iv_comment_zan = (ImageView) view.findViewById(R.id.iv_comment_zan);
        }
    }

    public interface ItemCommentDetailsZanClick {
        void ItemDetailZanClick(int position, String coid);
    }

    public interface OnNameCLickDetail {
        void nameClickDetail(String uid);
    }

    public interface ItemClcik {
        void repeat(String coid, String name);
    }

    class Clickable extends ClickableSpan {
        private String uid;

        public Clickable(String s) {
            uid = s;
        }

        /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
            if (onNameCLickDetail != null)
                onNameCLickDetail.nameClickDetail(uid);
        }

        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#80cbc4"));
        }
    }
}
